﻿using OASystem.BLL.IService;
using OASystem.Common;
using OASystem.DAL;
using OASystem.Model;
using OASystem.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace OASystem.BLL.Service
{

    //记得修改Userinfo成自己要使用的实体
    public class AnnouncementService : BaseRepository<Announcement>, IAnnouncementService
    {
        public static Announcement AresponseResult = null;//单个
        public static List<Announcement> AresponseResults = new List<Announcement>();//多个
        /// <summary>
        /// 单个通知发布
        /// </summary>
        /// <returns></returns>
        #region 单个通知发布      
        public ResponseResult AnnouncementByOne(Announcement announcement, int departId)
        {


            //创建实例
            using (var db = new OasystemEntities1())
            {
                using (var tran = db.Database.BeginTransaction())
                {
                    try
                    {
                        //第一种事务
                        //添加第一张表
                        db.Announcement.Add(announcement);
                        //保存
                        db.SaveChanges();

                        //添加第二张表
                        Announcementrange _Announcementrangerange = new Announcementrange();
                        _Announcementrangerange.Ann_Id = announcement.Ann_Id;
                        _Announcementrangerange.DepartId = departId;

                        db.Announcementrange.Add(_Announcementrangerange);


                        //保存
                        db.SaveChanges();
                        //提交
                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        //回滚
                        tran.Rollback();

                    }
                }

            }
            //实例化ResponseResult对象
            ResponseResult responseResult = new ResponseResult();
            //给ResponseResult对象赋值
            responseResult.data = announcement;
            responseResult.success = true;
            AresponseResult = responseResult.data as Announcement;
            //提交到控制器处理
            return responseResult;
        }
        #endregion
        /// <summary>
        /// 多个通知发布
        /// </summary>
        /// <returns></returns>
        #region 多个通知发布      
        public ResponseResult AnnouncementByMore(Announcement announcement, int departId)
        {
            #region 查询部门下的所有人
            Access_listService access_ListService = new Access_listService();
            List<Userinfo> userinfos = new List<Userinfo>();
            userinfos = access_ListService.AnnouncementByMore(departId).data as List<Userinfo>;
            #endregion
            //循环遍历赋Publisher值且两表添加
            #region 循环遍历赋Publisher值且两表添加
            for (int i = 0; i < userinfos.Count; i++)
            {
                //给每个userinfos赋值
                announcement.Publisher = userinfos[i].UserName;
                AresponseResults.Add(announcement.DeepCloneObject());

                //创建实例
                using (var db = new OasystemEntities1())
                {
                    using (var tran = db.Database.BeginTransaction())
                    {
                        try
                        {
                            //第一种事务
                            //添加第一张表
                            db.Announcement.Add(announcement);
                            //给ResponseResult对象赋值
                            //保存
                            db.SaveChanges();
                            //添加第二张表
                            Announcementrange _Announcementrangerange = new Announcementrange();
                            _Announcementrangerange.Ann_Id = announcement.Ann_Id;
                            _Announcementrangerange.DepartId = departId;
                            db.Announcementrange.Add(_Announcementrangerange);
                            //保存
                            db.SaveChanges();
                            //提交
                            tran.Commit();
                            //提交到控制器处理
                        }
                        catch (Exception ex)
                        {
                            //回滚
                            tran.Rollback();

                        }
                    }
                }
            } 
            #endregion
            ResponseResult responseResult = new ResponseResult();
            responseResult.success = false;
            return responseResult;
        }
        #endregion
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="username"></param>
        /// <returns></returns>
        #region 分页查询
        public ResponseResult NoticPageList(int page, int limit, string username)
        {
            //创建ResponseResult对象
            ResponseResult responseResult = new ResponseResult();
            //ResponseResult对象分页属性赋值
            responseResult.code = 0;
            responseResult.msg = string.Empty;
            int count = 0;
            //Lambda表达式查询
            Expression<Func<Announcement, bool>> whereLambda = a => true;
            if (!string.IsNullOrEmpty(username))
            {
                whereLambda = a => a.Ann_Title.Contains(username);
            }
            Expression<Func<Announcement, int>> orderbyLambda = c => c.Ann_Id;
            responseResult.data = QueryPageList(page, limit, whereLambda, orderbyLambda, out count);
            responseResult.count = count;
            //提交ResponseResult对象前台处理
            return responseResult;
        }
        #endregion

    }
}
